package com.nova.view;

import com.nova.NovaConfig;
import com.nova.source.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class NovaTextDialog {
    private Dialog mDialog;
    private TextView mTextView;
    private boolean hasActivityFinish;
    private Activity activityFinish;
    private Runnable cancelRunnable = new Runnable() {
        public void run() {
            NovaTextDialog.this.cancel();
            if (hasActivityFinish) {
                activityFinish.finish();
            }
        }
    };

    public NovaTextDialog(Context context) {
        this.mDialog = new Dialog(context, R.style.dialog);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_text, null);
        this.mTextView = ((TextView) view.findViewById(R.id.textview));
        if ("0".equals(NovaConfig.textdialog_flag)) {
            // 0默锟斤拷锟斤拷色 1锟侥成猴拷色
        } else if ("1".equals(NovaConfig.textdialog_flag)) {
            this.mTextView.setTextColor(Color.BLACK);
        }
        this.mDialog.setCancelable(true);
        this.mDialog.setContentView(view);
        this.mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                NovaTextDialog.this.mTextView.removeCallbacks(NovaTextDialog.this.cancelRunnable);
            }
        });
        this.mDialog.show();
    }

    public void setText(String text) {
        this.mTextView.setText(text);
    }

    public void setText(int textID) {
        this.mTextView.setText(textID);
    }

    public void show() {
        this.mDialog.show();
        this.mTextView.postDelayed(this.cancelRunnable, 2000L);
    }

    public void cancel() {
        try {
            this.mDialog.cancel();
        } catch (IllegalArgumentException exception) {

        }
    }

    /**
     * 设置关闭Activity
     */
    public void setFinishActivity(Activity activity) {
        if (activity != null) {
            this.activityFinish = activity;
            hasActivityFinish = true;
        } else {
            hasActivityFinish = false;
        }
    }
}
